package org.example.fileupload.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.fileupload.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Locale;

/**
 * @Author zy
 * @Date 2024/12/20 14:23
 * @packageName:org.example.fileupload.controller
 * @ClassName:FileuploadController
 */
@RestController
@RequestMapping("/admin/report")
@CrossOrigin
@Api(tags = "文件")
public class FileuploadController{

    @Autowired
    private FileService fileService;
    //文件后缀
    String endName="";
    //原始路径
    String originNamefinal="";
    //保存路径
    String savePath="";
    //上传快慢的状态码
    Integer uploadStatus=1;

    @GetMapping("/slowupload")
    @ApiOperation("慢速上传")
    public void slowUpload(){
        System.out.println("一般上传中");
        uploadStatus=1;
    }
    @GetMapping("/fastupload")
    @ApiOperation("慢速上传")
    public void fastUpload(){
        System.out.println("快速上传中");
        uploadStatus=0;
    }
    @PostMapping("/upload")
    @ApiOperation("上传文件")
    public String upload(@RequestBody MultipartFile file) throws InterruptedException {
        long startTime=System.nanoTime();
        if(uploadStatus==1){
            Thread.sleep(3000);
        }
        String returnFileName="";
        try {
            //原始文件名
            String originalFileName = file.getOriginalFilename();
            System.out.println(originalFileName);
            originNamefinal=originalFileName;
            //获取.的位置
            int count=0;
            for(int i=0;i<originalFileName.length();i++){
                if(originalFileName.charAt(i)=='.'){
                    count=i;
                    break;
                }
            }

            //获取文件后缀
            endName=originalFileName.substring(count);
            System.out.println(endName);
            //保存路径
            savePath="D:\\exceltest";
            File folder=new File(savePath);
            if(!folder.exists()){
                folder.mkdirs();
            }

            String saveName=originalFileName;
            file.transferTo(new File(savePath,saveName));
            String fileName=savePath+"\\"+saveName;
            returnFileName=fileName;
            System.out.println("文件路径名："+returnFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        long endTime=System.nanoTime();
        System.out.println("上传时间："+(endTime-startTime)/1000000+"ms");
        return returnFileName;
    }

    @GetMapping("/clean")
    @ApiOperation("清除文件")
    public String clean(){
        boolean exists= Files.exists(Paths.get(savePath,originNamefinal));
        String oldPath=savePath+"\\"+originNamefinal;
        System.out.println(oldPath);
        if(exists){
            File file=new File(oldPath);
            System.out.println("清除文件："+oldPath);
            file.delete();
            return "success";
        }
        return "文件不存在";
    }

    @GetMapping("/filename")
    @ApiOperation("修改文件名字")
    public String getFileName(@RequestParam("username") String filename) throws IOException {
        boolean exists=Files.exists(Paths.get(savePath,originNamefinal));
        String oldPath=savePath+"\\"+originNamefinal;
        String newPath=savePath+"\\"+filename+endName;
        if (exists){
            Files.move(Paths.get(oldPath),Paths.get(newPath));
            originNamefinal=filename+endName;
            return "success";
        }
        return "文件不存在";
    }

    @GetMapping("/export")
    @ApiOperation("导出文件")
    public void export(HttpServletResponse response) throws IOException {
        System.out.println("导出成功");
        fileService.export(response);
    }


}
